wx.cloud.init()
Page({
  
  data: {
    listData: [],
    currentIdx: '',
    hiddenNametop: false,
    hiddenName: true,
    formData: {
      title: '',
      position: '',
      intime: '',
      total_number: '',
      stock_number: ''
    }
  },


  onLoad: function (options) {
    const db = wx.cloud.database()
    db.collection('data_book').get({
      success: res => {
        this.setData({
          listData: res.data
        })
      }
     
    })
    
  },
  click: function (e) {
    this.setData({
      formData: e.target.dataset.item,
      currentIdx: e.target.dataset.index,
      hiddenName: !this.data.hiddenName,
      hiddenNametop: !this.data.hiddenNametop,
    })


  },
  user: function () {
    wx.navigateTo({
      url: '../addlibrary/addlibrarym'
    })
  },
  librarymanagement: function () {
    wx.navigateTo({
      url: '/pages/studentInformation/studentInformation'
    })
  },
  formSubmit: function (e) {
   
    //拿到的数据将其与数据库中的学号进行匹配，然后进行数据库的更新，弹窗出来，显示成功
    const newData = this.data.listData;
    console.log(newData);
    newData[this.data.currentIdx].title = e.detail.value.title
    newData[this.data.currentIdx].position = e.detail.value.position
    newData[this.data.currentIdx].intime = e.detail.value.intime
    newData[this.data.currentIdx].total_number = e.detail.value.total_number
    newData[this.data.currentIdx].stock_number = e.detail.value.stock_number
    let title = e.detail.value.title;
   
    this.setData({
      listData: newData,
      hiddenName: !this.data.hiddenName,
      hiddenNametop: !this.data.hiddenNametop
    })
    const db = wx.cloud.database()
   //修改数据库
   db.collection('data_book').where({title}).update({
     data: {
       ...e.detail.value
     },
     success(data){
       console.log(data);
     }
   })
  },
  //删除
  del:function(e){
    const db = wx.cloud.database()
    const newData = e.target.dataset.item
    const index =e.target.dataset.index
   
    const suoyin = newData._id
    
   wx.showModal({
    title: '提示！',
    content: '确认删除',
    showCancel: false,
    success(res) {
      db.collection('data_book').where({
        _id:suoyin
      }).remove()
      wx.navigateTo({
        url: '/pages/adminInformation/adminInformation'
      })
    }
  })
  },

})